<template>
	<view class="container">
		<view class="search">
			<u-search bgColor="#fff" placeholder="请输入搜索" @custom="onSearch" v-model="keyword"></u-search>
		</view>
		<view class="slected">
			<view @click="onSelect(1)" class="item ">
				{{classTxt}}
			</view>
			<view  @click="onSelect(2)" class="item it2">
				{{dayTxt}}
			</view>
			<view  @click="onSelect(3)" class="item">
				{{suiTxt}}
			</view>
		</view>
		<view v-for="item in list" @click="onToDetail(item)" class="cell">
			<view class="cell-top">
				<img :src="baseUrl + item.activityPic" alt="">
			</view>
			<view class="cell-bot">
				<view class="cell-bot1">
					<view class="cell-bot1-l">
						{{item.activityTitle}}
					</view>
					<view class="cell-bot1-r">
						￥{{item.activityFee}}
					</view>
				</view>
				<view class="cell-bot2">
					{{item.activitySubTitle}}
				</view>
				<view class="cell-bot2">
					{{item.startDate}} ~ {{item.endDate}}
				</view>
			</view>
		</view>
		<u-picker :show="show" :columns="columns" @cancel="cancel" @confirm="confirm" keyName="label"></u-picker>
	</view>
</template>

<script>
	import { activityList,	login,  bind } from './indexServ.js'
	export default {
		name: 'template',
		data() {
			return{
				baseUrl: getApp().globalData.envParam.baseURL,
				keyword: '',
				show:false,
				classTxt: '分类',
				dayTxt: '日期',
				suiTxt: '年龄',
				columns: [],
				index: 0,
				list: [],
				params: {
					  "key": '',
					  "activityGroup": "",
					  "activityType": "",
					  "isAsc": "desc",
					  "orderByColumn": "id",
					  "pageNum": 1,
					  "pageSize": 100,
					  "timePeriod": ""
				},
				c1: [
					[
						{
							label: '不限分类',
							id: -1
						},
						{
							label: '热门赛事',
							id: 107
						},
						{
							label: '考级测评',
							id: 108
						},
						{
							label: '线下展览',
							id: 109
						},
						{
							label: '研学活动',
							id: 110
						},
						{
							label: '夏/冬令营',
							id: 111
						},
						{
							label: '素质课程',
							id: 112
						},
					]
				],
				c2: [
					[
						{
							label: '不限日期',
							id: -1
						},
						{
							label: '本两周',
							id: 1
						},
						{
							label: '近一个月',
							id: 2
						},
						{
							label: '近三个月',
							id: 4
						},
						{
							label: '近半年',
							id: 8
						}
						
					]
				],
				c3: [
					[
						{
							label: '不限年龄',
							id: -1
						},
						{
							label: '幼儿(6岁及以下)',
							id: 113
						},
						{
							label: '小学(7-12岁)',
							id: 114
						},
						{
							label: '初高中(13-18岁)',
							id: 115
						},
						{
							label: '成人(19岁及以上)',
							id: 116
						} 
					]
				],
			}
		},
		onLoad() {
			this.autoLogin()
			this.GET_activityList()
			
			uni.setStorageSync("channelId","1000")
		},
		// onShow() {
		// 	this.initLoad()
		// },
		methods: {
			confirm(e) {
                console.log('confirm', e)
				if(this.index == 1){
					this.classTxt = e.value[0].label
					this.params.activityGroup = e.value[0].id
				}
				if(this.index == 2){
					this.dayTxt = e.value[0].label
					this.params.activityType = e.value[0].id
				}
				if(this.index == 3){
					this.suiTxt = e.value[0].label
					this.params.timePeriod = e.value[0].id
				}
				this.GET_activityList()
                this.show = false
			},
			cancel() {
				this.show = false
			},
			onSearch(e) {
				console.log(e)
				this.params.key = e
				this.GET_activityList()
			},
			
			onSelect(index){
				if(index == 1){
					this.index = 1
					this.columns = this.c1
				}
				if(index == 2){
					this.index = 2
					this.columns = this.c2
				}
				if(index == 3){
					this.index = 3
					this.columns = this.c3
				}
				this.show = true
			},
			onToDetail(o) {
				uni.navigateTo({
					url: `/pages/detail/index?activityId=` + o.id,
					fail: error => {
						console.log('error；', error)
					}
				})
			},
			
			async GET_activityList() {
				let res = await activityList(this.params)
				if (res.code == 200) {
					this.list = res.data.rows
				}
			},
			autoLogin(e) {
				uni.login({
					provider:"weixin",
					success:function(loginRes){
						console.log(loginRes);
							// 获取用户信息
						const accountInfo = uni.getAccountInfoSync()
						const appid = accountInfo.miniProgram.appId	
						const code= loginRes.code
						const params={code,appid}
						console.log('当前小程序appId',appid)
						login(params).then(res=>{
							// const openId= res.data.openid;
							console.log('get OpenId:',res.openid)
							uni.setStorageSync('openid', res.openid)
							bind({openId:res.openid,appid }).then(res=>{
								console.log('openId入库:',res)
							}).catch((ex)=>{
								uni.showToast({
									icon:"fail",
									title:"获取用户信息失败"
								})
							})
							
						})	
					}
				})
			}

			
		}
	}
</script>
<style  lang="scss" scoped>
	.container{
		// padding: 20rpx;
		box-sizing: border-box;
	}
	.search{
		padding: 20rpx;
		box-sizing: border-box;
	}
	.slected{
		width: 100%;
		height: 80rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		padding: 10rpx 0;
		box-sizing: border-box;
		.item{
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.it2{
			border-left:  2rpx solid #e1e1e1;
			border-right: 2rpx solid #e1e1e1;
		}
	}
	.cell{
		width: 100%;
		background: #fff;
		display: flex;
		flex-direction: column;
		margin-bottom: 40rpx;
		.cell-top{
			width: 100%;
			height: 400rpx;
			// background: pink;
			img{
				width: 100%;
				height: 400rpx;
			}
		}
		.cell-bot{
			width: 100%;
			padding: 20rpx 0rpx;
			box-sizing: border-box;
			.cell-bot1{
				width: 100%;
				display: flex;
				align-items: center;
				padding: 20rpx 10rpx;
				box-sizing: border-box;
				.cell-bot1-l{
					flex: 1;
					font-size: 34rpx;
					color: #000;
					padding-right: 10rpx;
					box-sizing: border-box;
					word-break: break-all;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2; /* 超出几行省略 */
					overflow: hidden;
					border-right: 2rpx solid #f4f4f4;
				}
				.cell-bot1-r{
					width: 200rpx;
					font-size: 30rpx;
					color: #f6511d;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
			.cell-bot2{
				width: 100%;
				padding:0 10rpx;
				box-sizing: border-box;
				color: #a9a9a9;
				font-size: 30rpx;
			}
		}
	}
</style>